package com.wudl.windows;


import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.scala.DataStream;


/**
 * @ClassName : Window_CountWindow
 * @Description : 计数窗口
 * @Author :wudl
 * @Date: 2020-10-24 01:43
 */

public class Window_CountWindow {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<String> dataStreamSource = env.socketTextStream("192.168.1.180",8899);
        KeyedStream<Tuple2<String, Integer>, String> mapStream = dataStreamSource.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String s)  {
                return Tuple2.of(s,1);
            }
        }).keyBy(r -> r.f0);


        mapStream.countWindow(3).sum(1).print();
        // 滑动窗口: 两个参数，第一个是窗口长度，第二个是滑动步长
        mapStream.countWindow(3,2).sum(1).print();
        env.execute();



    }
}
